<?php
defined('BASEPATH') or exit('No direct script access allowed');


class Hunter extends MY_Controller
{
    public $poolnub1 = 0;
    public $poolnub2 = 0;


    public function __construct()
    {
        parent::__construct();
        if ($this->userinfo['group'] != 1 && $this->userinfo['group'] != 3) {
            $this->groupjunp($this->userinfo['group']);
        }
        $this->host = '';

    }

    //主页

    /**
     *
     */
    public function index()
    {
        $this->address = '测试员首页';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->model('Jd_task_jd_list_model');
        $a = $this->Jd_task_jd_list_model->getmonthdata($this->user_id);
        $days = explode("-", date('Y-m-d'));
        $days = intval($days[2]);
        $a['avg'] = round($a['all'] / $days, 2);
        $this->load->view('hunter/index', $a);
    }


    public function paylog()
    {

        $this->address = '测试员资金详情';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/hunterpaylog');


    }


    /**
     * 京东接单页面
     */
    public function getjdtask()
    {
        $this->address = '京东接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $redis = $this->MyRedis();
        $this->poolnub1 = $redis->get('poolnub1');


        $awz = $this->session->awz ? $this->session->awz : 0;
        $data['awz'] = $awz;
        $this->load->view('hunter/getjdtask', $data);
    }

    public function getpddtask()
    {
        $this->address = '拼多多接单（普通）';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $awz = $this->session->awz ? $this->session->awz : 0;
        $redis = $this->MyRedis();
        $this->poolnub2 = $redis->get('poolnub2');
        $data['awz'] = $awz;
        $this->load->view('hunter/getpddtask', $data);
    }


    public function getpddtaskh()
    {
        $this->address = '拼多多接单（高留评）';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $awz = $this->session->awz ? $this->session->awz : 0;
        $redis = $this->MyRedis();
        $this->poolnub2 = $redis->get('poolnub2');
        $data['awz'] = $awz;
        $this->load->view('hunter/getpddtaskh', $data);
    }


    public function gettask()
    {
        $this->address = '轮次接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/gettask.vue');
    }


    public function pddtasklist()
    {
        $this->address = '拼多多已完成';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/finlishlist');

    }

    public function finlishlist()
    {
        $this->address = '完成任务';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/finlishlist');
    }


    /**
     * 获取任务池中的任务
     */
    public function getjdtaskaction($type = 1)
    {
        $epage = $this->input->post('epage');
        $page = $this->input->post('page');
        $awz = $this->input->post('awz') ? $this->input->post('awz') : "";

        $shop_name = $this->input->post('shop_name') ? $this->input->post('shop_name') : "";

        $this->load->model('Jd_task_jd_list_pool_model');
        if ($epage > 20) {
            $epage = 20;
        }
        if ($this->group != 3 && $this->group != 1) {
            $msg['code'] = 1;
            $msg['msg'] = '你不是测试员无法获取任务';
            $this->sendjsonmsg($msg);
        }
        if (empty($epage)) {
            $epage = 3;
        }
        $this->user_id = $this->session->user_id;
        $where = "hunter_id=$this->user_id   AND `status` =1 AND type=$type";
        $list = $this->Jd_task_jd_list_pool_model->pageData($where, "*", $page, 100);

        $this->load->model('Jd_pic_model');
        // echo $this->db->last_query();die;
        if (!empty($list['list'])) {//如果查询到有在进行的任务直接返回当前任务
            $list['epage'] = 1;
            $list['page'] = $page;
            foreach ($list['list'] as $id => $v) {
                $list['list'][$id]['money'] = $v['price'] * $v['number'];
                if ($v['type'] == 1) {
                    $list['list'][$id]['linkid'] = 0;
                    $subject = '%.*?item.jd.com\\/(\d{0,20})%is';
                    $result = preg_match($subject, $v['link'], $row);
                    if ($result) {
                        $list['list'][$id]['linkid'] = $row[1];
                    }
                }
                $str = explode("/", $v['picurl']);
                if (isset($str[1]) && $str[1] == 'pic') {
                    $list['list'][$id]['picurl'] = base_url() . "public/uploads" . $v['picurl'];
                }
                if ($v['showpicid']) {
                    $showpic = $this->Jd_pic_model->getonebyid($v['showpicid']);
                    if ($showpic) {
                        $list['list'][$id]['showpicid'] = base_url() . "public/uploads" . $showpic['url'];
                    }
                }

                if ($v['comment']) {
                    $showcomment = $this->Jd_pic_model->gettextbid($v['comment']);

                    if ($showcomment) {
                        $list['list'][$id]['text'] = $showcomment[0]['text'];
                    }
                }
            }
            if (!isset($this->session->awz) || $this->session->awz < 1) {
                $this->session->awz = count($list['list']);
            }
            $list = $this->checklist($list);
            if (empty($list)) {
                $msg['code'] = 1;
                $msg['msg'] = '获取任务失败 请重新再试';
                $this->session->awz = 0;
                $this->sendjsonmsg($msg);
            }
            $this->sendjsonmsg($list);
        }
        $this->db->trans_begin();

        $list = $this->Jd_task_jd_list_pool_model->HunterGetTask($epage, "", $type, $shop_name);


        $list['epage'] = $epage;
        $list['page'] = $page;
        if (!empty($list['list'])) {
            $idlist = array();
            foreach ($list['list'] as $id => $v) {
                $idlist[] = $v['id'];
                $list['list'][$id]['real_price'] = "";
            }
            $result = $this->Jd_task_jd_list_pool_model->HunterAccept($this->user_id, $idlist, $awz);
            if (!$result) {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '获取任务失败 请重新再试';
                $this->sendjsonmsg($msg);
            }
            $this->db->trans_commit();
        }
        if (empty($list['list'])) {
            $this->session->awz = 0;
            $msg['code'] = 1;
            $msg['msg'] = '暂无合适任务';
            $this->sendjsonmsg($msg);
        }

        $this->sendjsonmsg($list);
    }


    public function getTurnTask()
    {
        $epage = $this->input->post('epage');
        $page = $this->input->post('page');
        $awz = $this->input->post('awz') ? $this->input->post('awz') : "";
        $turn = $this->input->post('turn') ? $this->input->post('turn') : "";
        $this->load->model('Jd_task_jd_list_pool_model');
        if ($epage > 20) {
            $epage = 20;
        }
        if ($this->group != 3 && $this->group != 1) {
            $msg['code'] = 1;
            $msg['msg'] = '你不是测试员无法获取任务';
            $this->sendjsonmsg($msg);
        }
        if (empty($epage)) {
            $epage = 3;
        }
        $this->user_id = $this->session->user_id;
        $where = "hunter_id=$this->user_id   AND `status` =1";
        $list = $this->Jd_task_jd_list_pool_model->pageData($where, "*", $page, 100);

        $this->load->model('Jd_pic_model');
        // echo $this->db->last_query();die;
        if (!empty($list['list'])) {//如果查询到有在进行的任务直接返回当前任务
            $list['epage'] = 1;
            $list['page'] = $page;
            foreach ($list['list'] as $id => $v) {

                $str = explode("/", $v['picurl']);
                if (isset($str[1]) && $str[1] == 'pic') {
                    $list['list'][$id]['picurl'] = base_url() . "public/uploads" . $v['picurl'];
                }
                $list['list'][$id]['orderid'] = date('ymd') . "-";
            }
            if (empty($list)) {
                $msg['code'] = 1;
                $msg['msg'] = '获取任务失败 请重新再试';
                $this->session->awz = 0;
                $this->sendjsonmsg($msg);
            }
            $this->sendjsonmsg($list);
        }


        $this->db->trans_begin();
        $list = $this->Jd_task_jd_list_pool_model->HunterGetTaskTurn($epage, $turn, $this->user_id);


        $list['epage'] = $epage;
        $list['page'] = $page;
        if (!empty($list['list'])) {
            $idlist = array();
            foreach ($list['list'] as $id => $v) {
                $idlist[] = $v['id'];
                $str = explode("/", $v['picurl']);
                if (isset($str[1]) && $str[1] == 'pic') {
                    $list['list'][$id]['picurl'] = base_url() . "public/uploads" . $v['picurl'];
                }
                $list['list'][$id]['real_price'] = "";
                $list['list'][$id]['orderid'] = date('ymd') . "-";
            }
            $result = $this->Jd_task_jd_list_pool_model->HunterAccept($this->user_id, $idlist, $awz = "", $turn);
            if (!$result) {
                $this->db->trans_rollback();
                $msg['code'] = 1;
                $msg['msg'] = '获取任务失败 请重新再试';
                $this->sendjsonmsg($msg);
            }
            $this->db->trans_commit();
        }
        if (empty($list['list'])) {
            $msg['code'] = 1;
            $msg['msg'] = '暂无任务';
            $this->sendjsonmsg($msg);
        }
        $this->sendjsonmsg($list);
    }


    public function getpddtaskaction()
    {
        $this->getjdtaskaction(2);
    }

    public function checklist($list)
    {
        $group = array();
        foreach ($list['list'] as $id => $v) {
            if (isset($group[$v['shop_name']])) {
                $group[$v['shop_name']] = $group[$v['shop_name']] + 1;
            } else {
                $group[$v['shop_name']] = 1;
            }
        }
        foreach ($group as $id => $v) {
            if ($v > 1) {
                $this->load->model('Jd_task_jd_list_pool_model');
                $this->Jd_task_jd_list_pool_model->HunterUnaccept($this->user_id);
                return array();
            }
        }
        return $list;
    }


    /**
     *
     * 测试员完成任务页面
     */
    public function jdfinishlist()
    {
        $this->address = '京东接单';
        $this->load->view('hunter/head');
        $this->load->view('hunter/left');
        $this->load->view('hunter/jdfinishlist');
    }


    /**
     * 获取已完成的任务（京东）
     */
    public function getjdfinishlist($type = 1)
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        $getexcel = $this->input->post('getexcel');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_task_jd_list_model');
        $where = " where   hunter_id=" . $this->user_id . " AND (status=2 or status>4)";
        $shop_name = $this->input->post('shop_name') ? $this->input->post('shop_name') : "";

        $where = $where . $this->getsqlstrbyrow(['type_h', 'orderid', 'awz', 'taskid', 'overnight']);
        $where = $where . $this->gettimesqlstr('finishtime');
        $where = $where . $this->getsqlstrbyrow(['username'], "p4");
        if (!empty($shop_name)) {
            $shop_name = trim($shop_name);
            $where .= " AND p1.shop_name like '%$shop_name%'";
        }
        $_select = " p1.*";
        $_order = " order by p1.finishtime desc ";
        $sql = "select SELECT from `jd_task_jd_list` as p1                   
                     
			 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $where,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_task_jd_model');
        $a = $this->Jd_task_jd_model->get_server_list($_data);


        $teamlist = $this->GetCachData('getteamlist', 1);
        $this->load->model('Jd_pic_model');
        foreach ($a['list'] as $id => $v) {
            if ($v['status'] == 0) {
                $a['list'][$id]['finishtime'] = "";
                $a['list'][$id]['status'] = "未完成";
                $a['list'][$id]['account'] = "";
                $a['list'][$id]['real_price'] = "";
                $a['list'][$id]['orderid'] = "";
            } else {
                $a['list'][$id]['status'] = "完成";
                $a['list'][$id]['finishtime'] = date("Y-m-d H:i:s", $v['finishtime']);
            }
            $a['list'][$id]['type_hname'] = $v['type_h'] == 1 ? "普通单" : "高留评";

            @$a['list'][$id]['teamname'] = $teamlist[$v['team']];


            if ($v['comment']) {
                $showcomment = $this->Jd_pic_model->gettextbid($v['comment']);


                if ($showcomment) {
                    $a['list'][$id]['text'] = $showcomment[0]['text'];
                }
            }

        }

        if ($getexcel) {
            $str['id'] = ['ID'];
            $str['addtime'] = ['完成时间', 20];
            $str['orderid'] = ['订单号', 30];
            $str['hunter_name'] = ['测试员'];
            $str['shop_name'] = ['店铺名', 20];
            $str['taskid'] = ['任务ID'];
            $str['teamname'] = ['团队'];
            $str['real_price'] = ['实际付款'];
            $str['awz'] = ['awz'];
            $str['overnight'] = ['隔夜'];
            $str['number'] = ['总单量'];
            $str['keyword'] = ['关键字', 30];
            $str['price'] = ['录入价格'];
            $str['number'] = ['每单数量'];
            $this->excel_out2($str, $a['list'], "1");
        }
        $this->sendjsonmsg($a);
    }


    public function getpddfinishlist()
    {
        $this->getjdfinishlist(2);
    }


    /**
     * 导出测试员当天详细记录
     */
    public function finishlistexcel($type = 2, $date = "")
    {
        $page = 1;
        $epage = 10000;
        $this->load->model('Jd_task_jd_list_model');
        $where = " hunter_id=" . $this->user_id . " AND status=2 AND `type`=$type";
        $orderid = $this->input->post('orderid') ? $this->input->post('orderid') : "";
        $awz = $this->input->post('awz') ? $this->input->post('awz') : "";
        $date = $this->input->post('date') ? $this->input->post('date') : date('Y-m-d');

        if ($orderid) {
            $where .= " AND orderid='$orderid'";
        }
        if ($awz) {
            $where .= "  AND awz='$awz'";
        }
        if ($date) {
            $data1 = date('Y-m-d 00:00:00', strtotime($date));
            $data1 = strtotime($data1);
            $data2 = date('Y-m-d 23:59:59', strtotime($date));
            $data2 = strtotime($data2);
            $where .= " AND finishtime>$data1 AND  finishtime<$data2";
        }
        $order['shop_name'] = "DESC";


        $select = "id,finishtime,shop_name,number,keyword,price,orderid,awz,sku,hunter_name,type,real_price";
        $a = $this->Jd_task_jd_list_model->pageData($where, $select, $page, $epage);

        $a['epage'] = $epage;
        $a['page'] = $page;
        foreach ($a['list'] as $id => $v) {
            $a['list'][$id]['finishtime'] = date("Y-m-d H:i:s", $v['finishtime']);
            if ($v['type'] == 1) {
                $a['list'][$id]['type'] = "京东";
            } else {
                $a['list'][$id]['type'] = "拼多多";
            }
        }
        $str[] = 'ID';
        $str[] = '完成时间';
        $str[] = '店铺名';
        $str[] = '每单数量';
        $str[] = '关键字';
        $str[] = '录入价格';
        $str[] = '订单号';
        $str[] = 'AWZ';
        $str[] = 'SKU';
        $str[] = '测试员';
        $str[] = '类型';
        $str[] = '实际付款';
        $this->excel_out($str, $a['list'], "测试员今日订单记录");
    }


    /**
     * 已完成任务修改
     */
    public function JdEdite()
    {
        $row = $this->input->post();
        $id = $row['id'];
        $this->load->model('Jd_task_jd_list_model');
        $result = $this->Jd_task_jd_list_model->getonebyid($id);
        if (!isset($result['hunter_id'])) {
            $msg['code'] = 1;
            $msg['msg'] = "不存在该订单";
            $this->sendjsonmsg($msg);
        }
        if ($result['status'] != 2) {
            $msg['code'] = 1;
            $msg['msg'] = "该订单还未完成";
            $this->sendjsonmsg($msg);
        }
        if ($result['hunter_id'] != $this->user_id) {
            $msg['code'] = 2;
            $msg['msg'] = "你不是该订单的测试员禁止修改";
            $this->sendjsonmsg($msg);
        }
        if ($result['orderid'] == $row['orderid'] && $result['real_price'] == $row['real_price'] && $result['awz'] == $row['awz']) {
            $msg['code'] = 2;
            $msg['msg'] = "参数未变";
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_begin();
        $a = $this->Jd_task_jd_list_model->updaterealmoney($id, $row);
        if ($a) {
            $newrow['user_id'] = $this->user_id;
            $newrow['oldprice'] = $result['real_price'];
            $newrow['oldorderid'] = $result['orderid'];
            $newrow['newprice'] = $row['real_price'];
            $newrow['neworderid'] = $row['orderid'];
            $newrow['date'] = date('Y-m-d');
            $newrow['type'] = 1;
            $newrow['logid'] = $id;
            $newrow['addtime'] = time();
            $this->load->model('Jd_hunter_log_model');
            $this->Jd_hunter_log_model->addlog($newrow);
            $this->load->model('Jd_task_jd_model');
            $this->Jd_task_jd_model->updatetaskmoney($result['taskid']);
            $row = array();
            $row['money'] = $newrow['oldprice'] - $newrow['newprice'];
            $row['log_id'] = $id;
            $row['user_id'] = $this->user_id;
            $r = $this->moneyupdate($row, 7, $id);
            if (!$r) {
                return false;
            }
            /*    $r= $this->huntertotal($row);
                if(!$r) {
                    return false;
                }*/
            $this->db->trans_commit();
            $this->sendjsonmsg();
        }


    }


    /**
     * 测试员完成任务
     */
    public function finnish()
    {
        $project = $this->input->post('project') ? $this->input->post('project') : 1;
        if (empty($project)) {
            $msg['code'] = 1;
            $msg['msg'] = '缺少项目标示';
            $this->sendjsonmsg($msg);
        }
        $orderid = $this->input->post('orderid');
        if (strlen($orderid) > 23) {
            $msg['code'] = 1;
            $msg['msg'] = '订单错误';
            $this->sendjsonmsg($msg);
        }

        $this->project = $project;
        $this->db->trans_begin();

        $result = $this->hunterfinishjd($project);

        if (!$result) {
            $this->db->trans_rollback();
            $msg['code'] = 1;
            $msg['msg'] = '任务提交失败';
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_commit();

        $redis = $this->MyRedis(); //更新任务池中的任务数
        $redis->update('poolnub' . $project, -1);
        $msg['code'] = 0;
        $msg['msg'] = '任务完成';
        $this->sendjsonmsg($msg);
    }


    /**
     * 提交完成的任务
     * @return bool
     */
    public function hunterfinishjd($project = 1)
    {
        $real_price = $this->input->post('real_price');
        $id = $this->input->post('id');
        $orderid = $this->input->post('orderid');
        $awz = $this->input->post('awz');

        $this->load->model('Jd_task_jd_list_pool_model');
        $row = $this->input->post();

        $row['team'] = $this->userinfo['team'] ? $this->userinfo['team'] : "";
        $row['username'] = $this->userinfo['username'];

        /*$result=$this->checkmoney($real_price,0);
        if(!$result){
            $msg['code'] =  1;
            $msg['msg'] =  '余额不足';
            $this->sendjsonmsg($msg);
        }*/
        $result = $this->Jd_task_jd_list_pool_model->finishtask($this->session->user_id, $row);
        unset($row['team']);
        unset($row['username']);
        if (!$result) {
            return false;
        }
        $r = $this->checktaskend($row);//商户主任务更新
        if (!$r) {
            return false;
        }
        /*  $row['task_nub_finnish']=1;
          $r=$this->UpdataUserinfo($row,$this->user_id);//
          if(!$r) {
              return false;
          }

          $row=array();
          $row['money']=$real_price;
          $row['log_id']=$orderid;
          $row['user_id']=$this->user_id;
          $r=$this->moneyupdate($row,4,$id);
          if(!$r) {
              return false;
          }
          $r= $this->huntertotal($row);
          if(!$r) {
              return false;
          }*/
        if (isset($this->session->awz)) {
            $this->session->awz = $this->session->awz - 1;
            if ($this->session->awz == 0) {
                unset($this->session->awz);
            }
        }
        return true;
    }


    /**
     * 计算测试员的总计
     */
    public function huntertotal($row)
    {
        foreach ($row as $id => $v) {
            if ($id != 'user_id' && $id != "money" && $id != "finishnub") {
                unset($row[$id]);
            }
        }
        $this->load->model('Jd_hunter_total_day_model');
        $resutl = $this->Jd_hunter_total_day_model->getupodate($row);
        if ($resutl) {
            $this->load->model('Jd_hunter_total_month_model');
            $resutl = $this->Jd_hunter_total_month_model->getupodatemoth($row);
        }
        return $resutl;
    }

    /**
     * 检查和更新任务进度
     */
    public function checktaskend($row)
    {
        $this->load->model('Jd_task_jd_list_pool_model');
        $row = $this->input->post();
        $result = $this->Jd_task_jd_list_pool_model->getonebyid($row['id']);
        $this->load->model('Jd_task_jd_model');
        return $this->Jd_task_jd_model->updatetask($result['taskid']);
    }


    /**
     *
     * 替换任务
     */
    public function repalcetask($type = 1)
    {
        $epage = $this->input->post('epage');
        $page = $this->input->post('page');
        $awz = $this->input->post('awz');
        $id = $this->input->post('id');


        $this->load->model('Jd_task_jd_list_pool_model');
        $this->user_id = $this->session->user_id;
        $row = $this->Jd_task_jd_list_pool_model->pageData(array('compare' => array('hunter_id' => $this->user_id, 'status' => 1, 'type' => $type)), "*", 1, 100);
        if (empty($row['list'])) {
            $msg['code'] = 1;
            $msg['msg'] = '没有任务可以重置';
            $this->sendjsonmsg($msg);
        }
        $idstr = array();
        $shopstr = array();
        foreach ($row['list'] as $id => $v) {
            $shopstr[] = $v['shop_name'];
        }
        $a = $this->Jd_task_jd_list_pool_model->HunterGetTask(1, $shopstr, $type);
        if ($a['list']) {
            $idstr[] = $this->input->post('id');
            $r = $this->Jd_task_jd_list_pool_model->replacetask($idstr);
        }
        $a['epage'] = $epage;
        $a['page'] = $page;
        if (!empty($a['list'])) {
            $idlist = array();
            $listid = array();
            foreach ($a['list'] as $id => $v) {
                $idlist[] = $v['id'];
                $listid[] = $v['listid'];
            }
            $this->Jd_task_jd_list_pool_model->HunterAccept($this->user_id, $idlist, $awz);
            $this->getjdtaskaction($type);
            /*    if(!isset( $this->session->awz) ||$this->session->awz==0 ){
                    $this->session->awz=count($a['list']);
                }

                $this->sendjsonmsg($a);*/
        } else {
            $a['code'] = 1;
            $a['msg'] = "暂无合适任务";
            $this->sendjsonmsg($a);
        }

    }


    /**
     *
     * 放弃任务
     */
    public function throwtask($type = 1)
    {
        $id = $this->input->post('id');
        $this->load->model('Jd_task_jd_list_pool_model');
        $this->user_id = $this->session->user_id;
        $row = $this->Jd_task_jd_list_pool_model->pageData(array('compare' => array('id' => $id, 'hunter_id' => $this->user_id, 'status' => 1, 'type' => $type)), "*", 1);
        if (empty($row['list'])) {
            $msg['code'] = 1;
            $msg['msg'] = '该任务不可放弃';
            $this->sendjsonmsg($msg);
        }


        $r = $this->Jd_task_jd_list_pool_model->replacetask($id);
        if ($r) {
            $this->session->awz = $this->session->awz - 1;
            $this->sendjsonmsg();
        } else {
            $a['code'] = 1;
            $a['msg'] = "放弃失败";
            $this->sendjsonmsg($a);
        }
    }


    public function getpaylog()
    {
        $page = $this->input->post('page');
        $epage = $this->input->post('epage');
        if (empty($epage)) {
            $epage = 10;
        }
        $this->load->model('Jd_pay_log_model');
        $username = $this->input->post('username') ? $this->input->post('username') : "";
        $date1 = $this->input->post('date1') ? $this->input->post('date1') : "";
        $date2 = $this->input->post('date2') ? $this->input->post('date2') : "";
        $_sql = " where  user_id= " . $this->user_id;
        if ($date1) {
            $date1 = strtotime($date1);
            $_sql .= " AND  p1.addtime>$date1 ";
        }
        if ($date2) {
            $date2 = strtotime($date2);
            $_sql .= " AND p1.addtime>$date2 ";
        }
        if (!empty($username)) {
            $_sql .= " AND  p2.username= '$username'";
        }
        $order['id'] = "DESC";
        $_select = " p1.*,p2.username";
        $_order = " order by p1.id desc ";
        $sql = "select SELECT from `jd_pay_log` as p1
                left join  `jd_users` as p2  on   p1.user_id=p2.id                  
				 SQL ORDER LIMIT";
        $_data = array("sql" => $sql,
            "_sql" => $_sql,
            "_select" => $_select,
            "_order" => $_order,
            "limit" => "",
            "page" => $page,
            "epage" => $epage);

        $this->load->model('Jd_task_jd_model');
        $a = $this->Jd_task_jd_model->get_server_list($_data);
        foreach ($a['list'] as $id => $v) {
            $a['list'][$id]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            switch ($v['type']) {
                case 1 :
                    $a['list'][$id]['type'] = '充值';
                    break;
                case 2 :
                    $a['list'][$id]['type'] = '发布任务';
                    break;
                case 3 :
                    $a['list'][$id]['type'] = '推荐奖励';
                    break;
                case 4 :
                    $a['list'][$id]['type'] = '测试员刷单';
                    break;
                case 5 :
                    $a['list'][$id]['type'] = '兑换金币';
                    break;
                case 6 :
                    $a['list'][$id]['type'] = '后台调整';
                    break;
            }
        }
        $this->sendjsonmsg($a);

    }


    function excelToArray()
    {

        if (isset($_FILES['uploadFile'])) {
            $file = $_FILES['uploadFile'];//得到传输的数据
        } else {
            $file = $_FILES['file'];//得到传输的数据
        }

        $msg['code'] = 1;

        if ($file['error'] > 0) {
            $msg['msg'] = "错误类型" . $file['error'];
            $this->sendjsonmsg($msg);
        }

        $name = $file['name'];
        $type = strtolower(substr($name, strrpos($name, '.') + 1)); //得到文件类型，并且都转化成小写
        $allow_type = array('xls'); //定义允许上传的类型

        if (!in_array($type, $allow_type)) {
            $msg['msg'] = "不允许上传" . $type . "型的文件";
            $this->sendjsonmsg($msg);
        }
        if (!is_uploaded_file($file['tmp_name'])) {
            return;
        }
        @$row = $this->excel_get($file['tmp_name']);
        if (empty($row)) {
            $msg['msg'] = "数据为空";
            $this->sendjsonmsg($msg);
        }

        $all = count($row);
        $taskid = $row[0]['J'];
        $this->load->model('Jd_task_jd_model');
        $status = $this->Jd_task_jd_model->getonebyid($taskid, 'status,number,pay_price,each_single');
        foreach ($row as $id => $v) {
            if (!$v['A']) {
                unset($row[$id]);
            }
        }
        if (!$status || $status['status'] != 1) {
            $msg['msg'] = "不存在该订单或者订单已完成";
            $this->sendjsonmsg($msg);
        }
        if ($status['number'] != $all) {
            $msg['msg'] = "订单单量不匹配";
            $this->sendjsonmsg($msg);
        }
        $allreal_price = 0;
        foreach ($row as $id => $v) {
            if (empty($v['A'])) {
                $msg['msg'] = "缺少ID";
                $this->sendjsonmsg($msg);
            }
            if (empty($v['D'])) {
                $msg['msg'] = "缺少订单号";
                $this->sendjsonmsg($msg);
            }
            $row[$id]['F'] = "excel";
            if (empty($v['I'])) {
                $msg['msg'] = "缺少实际付款";
                $this->sendjsonmsg($msg);
            }
            $allreal_price = $allreal_price + $v['I'];
        }

        $imbalance = $status['pay_price'] * $status['number'] * $status['each_single'] - $allreal_price;
        $res = $this->checkmoney($allreal_price, 0);
        if (!$res) {
            $msg['msg'] = "余额不足";
            //$this->sendjsonmsg($msg);
        }
        $this->load->model('Jd_task_jd_list_model');
        $this->db->trans_begin();
        $user_id = $this->user_id;
        $huntername = $this->username;
        $team = $this->userinfo['team'];
        foreach ($row as $id => $v) {
            $listid = $v['A'];
            $real_price = $v['I'];
            $awz = $v['F'];
            $orderid = $v['D'];
            $address = isset($v['K']) ? $v['K'] : "";
            $finishtime = time();
            @$sql = "update  jd_task_jd_list set `status`=2,hunter_id=$user_id, `real_price`=$real_price ,`awz`='$awz',  `orderid`='$orderid' ,`finishtime`=$finishtime,
`team`='$team',hunter_name='$huntername' ,addrees='$address' where id =$listid";


            $r = $this->db->query($sql);
            if (!$r) {
                $msg['msg'] = "任务更新错误";
                $this->sendjsonmsg($msg);
            }
            $updaterow = array();
            $updaterow['money'] = $real_price;
            $updaterow['log_id'] = $orderid;
            $updaterow['user_id'] = $this->user_id;
            $r = $this->moneyupdate($updaterow, 4, $id);
            if (!$r) {
                $msg['msg'] = "日志更新失败";
                $this->sendjsonmsg($msg);
            }
        }
        $finishtime = time();
        $sql = "update jd_task_jd set `status`=3 ,finish_nub= $all, finishtime=$finishtime , real_money=$allreal_price,imbalance=$imbalance,lastsendtime= $finishtime where id=$taskid ";
        $r = $this->db->query($sql);
        //$r=$this->Jd_task_jd_model->finnshtypeh($taskid,$row);
        if (!$r) {
            $msg['msg'] = "任务状态更新失败";
            $this->sendjsonmsg($msg);
        }
        $row = array();
        $row['task_nub_finnish'] = $all;
        $r = $this->UpdataUserinfo($row, $this->user_id);

        if (!$r) {
            $msg['msg'] = "刷手单量更新失败";
            $this->sendjsonmsg($msg);
        }
        $row = array();
        $row['user_id'] = $this->user_id;
        $row['money'] = $allreal_price;
        $row['finishnub'] = $all;
        $r = $this->huntertotal($row);
        if (!$r) {
            $msg['msg'] = "刷手总计数据更新失败";
            $this->sendjsonmsg($msg);
        }
        $this->db->trans_commit();
        $msg['code'] = 0;
        $msg['msg'] = "任务提交成功";
        $this->sendjsonmsg($msg);

    }


}
